//
//  PlayerController.m
//  TribleWar
//
//  Created by ＣＳＴ ＣＳＴ on 8/8/12.
//  Copyright (c) 2012 ＣＳＴ. All rights reserved.
//

#import "PlayerController.h"
#import "GameMove.pb.h"
#import "AppDelegate.h"
#define SPEED 50
@implementation PlayerController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        GameView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        GameView.backgroundColor = [UIColor whiteColor];
        players_ = [NSMutableDictionary dictionary]; 
    }     
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moveAnchorPoint:) userInfo:(id)player1 repeats:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    players_ = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)loadView
{
    self.view = GameView;
}

//get coordinate of the touch point 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    //get position of the animating layer
    CGPoint currentPosition = [[player_ presentationLayer]position];
    player_.curCoord = currentPosition;

    //set coordinate of touch to purpose coordinate
    UITouch *touch = [touches anyObject];
    CGPoint pos = [touch locationInView:self.view];
    player_.posCoord = pos;
    //[player_ moveAnchorPoint];
    
    
    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    int curTime = [[NSNumber numberWithDouble: timeStamp]intValue];
    
    Vector *curPos = [[[[Vector builder]setX:player_.curCoord.x]setY:player_.curCoord.y]build];
    Vector* purPos = [[[[Vector builder]setX:player_.posCoord.x]setY:player_.posCoord.y]build];
    State* state = [[[[[State builder]setCurPos:curPos]setPurPos:purPos]setCurTime:curTime]build];
    NSData* moveData = [[[[[[[Move builder]setCategory:CategoryCharacter]setState:state]setState:state]setUid:[player_.pid intValue]]build] data];
    NSLog(@"move ...");
    [[AppDelegate sharedAppDelegate]->netWorkHandler_ sendData:moveData Pid:PIDMove];

}

- (void)moveCallback:(Move *)move
{ 
    NSLog(@"move call back %llu", move.uid);
    if (!move) {
        NSLog(@"move is nil");
        return;
    }
    NSString *uid =[NSString stringWithFormat:@"%d",move.uid];
    Sprite *_player;

    if (![players_ objectForKey:uid]) {
        _player = [[Sprite alloc]init];
        [_player setPid:uid];

        [self addPlayer:_player];
    }else{
        _player = [players_ objectForKey:uid];
    }
    [_player setCurCoord:CGPointMake(move.state.curPos.x, move.state.curPos.y)];
    [_player setPosCoord:CGPointMake(move.state.purPos.x, move.state.purPos.y)];
    [_player moveAnchorPoint];

}

- (void)addPlayer:(Sprite*)player
{
    if (!player) {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([player.pid isEqualToString: [defaults objectForKey :@"userAccount"] ]) {
        player_ = player;
    }
    [players_ setValue:player forKey:player.pid];
    [GameView.layer addSublayer:player];
    
}

- (void)movePlayer:(Sprite*)player
{
    if (!player) {
        return;
    }
    [players_ removeObjectForKey:player.pid];
    [player removeFromSuperlayer];
}
@end
